1444A - Division - CodeForces Solution


brute force math number theory *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p;
int q,T;
vector<int> vc;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld %d",&p,&q);
		if(p % q)
		{
			printf("%lld\n",p);
			continue;
		}
		ll ans = 1;
		vc.clear();
		for(int i=2;i*i<=q;i++)
			if(q % i == 0)
				vc.push_back(i),vc.push_back(q/i);
		vc.push_back(q);
		for(int i=0;i<(signed)vc.size();i++)
		{
			ll r = p; 
			while(r % q == 0)	r /= vc[i];
			ans = max(ans,r);
		}
		printf("%lld\n",ans);
	}
 }

   		   			 							    					


Comments

Submit
0 Comments
More Questions

1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction
159C - String Manipulation 10
258A - Little Elephant and Bits
1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate